<?php
//include the information needed for the connection to SQLite filename and pseudo-database server. 
// we store here username, database and password 
require_once($_SERVER['DOCUMENT_ROOT'] . "/config.php");
include(ABS_PATH . "misc/functions.php");

function updateRow()
{
	global $db;
	
	$query = "UPDATE Inventory SET Name = '" . utf8_decode($_REQUEST['Name']) . "'," 
			. "Type = '" . $_REQUEST['Type'] . "'," 
			. "Permissions = '" . $_REQUEST['Permissions'] . "',"
			. "LastUpdate = '" . $_REQUEST['LastUpdate'] . "',"
			. " WHERE UUID = '" . utf8_decode($_REQUEST['UUID']) . "'";

	if ($db->query($query))
	{
		_log(__FILE__ . " - line " . __LINE__ . ": Database updated with query '$query'");
	}
	else
	{
		_log(__FILE__ . " - line " . __LINE__ . ": Error updating database with query '$query': " . $db->lastErrorMsg());
		exit(0); // flag jQueryGrid that this failed
	}
}

function addRow()
{
	global $db;

	// adding row not supported	
	{
		_log(__FILE__ . " - line " . __LINE__ . ": Adding row not supported: " .
					print_r($_REQUEST, TRUE));
		exit(0); // flag jQueryGrid that this failed
	}	
}

/* Main starts here */

// Get DB connection data

// connect to the SQLite database 

try {
	$db = new PDO(PDO_PREFIX . SQLITE_DB_FILENAME);
}

catch(PDOException $e)
{
	_log(__FILE__ . " - line " . __LINE__ . ": Database connection error: " . PDO_PREFIX . SQLITE_DB_FILENAME . ": " . $e->getMessage());
	exit(0);	// flag jQueryGrid that this failed
}

/*if (!($db = new SQLite3(SQLITE_DB_FILENAME)))
{
	_log(__FILE__ . " - line " . __LINE__ . ": Database connection error: " . $db->lastErrorMsg());
	exit(0);	// flag jQueryGrid that this failed
}*/
/*else
{
	_log(__FILE__ . " - line " . __LINE__ . ": Connected to database");
}*/

_log(__FILE__ . " - line " . __LINE__ . ": " . isset($_REQUEST['oper'])? $_REQUEST['oper'] : "No operation found!");

if (isset($_REQUEST['oper']))
{
	try {
		switch ($_REQUEST['oper'])
		{
			case 'del':
				{
					_log(__FILE__ . " - line " . __LINE__ . ": Inventory request for deletion: " .
						print_r($_REQUEST, TRUE));
						
					// explode, then implode adding quotes
					
					$deleteUUID = explode(",", utf8_decode($_REQUEST['id']));
					$deleteParameters = '"' . implode('","', $deleteUUID) . '"';
					
					
					if ($db->query("DELETE FROM Inventory WHERE UUID IN (" . $deleteParameters . ")"))
					{
						_log(__FILE__ . " - line " . __LINE__ . ": Inventory(s) deleted: " . $deleteParameters);
					}
					else
					{
						_log(__FILE__ . " - line " . __LINE__ . ": Error deleting inventory(s) \"" . $deleteParameters .
							 "\"! " . $db->lastErrorMsg());
						exit(0);
					}		
				}
				break;
			case 'edit':
				updateRow();
				break;
			case 'add':
				addRow(); // unsupported
				break;
		}		
	}
	catch(PDOException $e)
	{
		_log(__FILE__ . " - line " . __LINE__ . ": Some query failed: " . PDO_PREFIX . SQLITE_DB_FILENAME . ": " . $e->getMessage());
		exit(0);	// flag jQueryGrid that this failed
	}
	
	
}
else
{
	updateRow();
}
?>